using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using Microsoft.DirectX.DirectDraw;
using TowerDefense_Dx.Core;

namespace TowerDefense_Dx.GameLogic
{
    public class Level
    {
        private int _cellWidth;
        private int _cellHeight;
        private int _row = 12;
        private int _collum = 16;
        private int[,] _colisonMap = new int[12,16];
        public int[,] ColisionMap
        {
            get { return _colisonMap; }
        }
        private int[,] _tileMap = new int[12,16];
        private DxInitGraphics _graphics;
        private DxSprite _texture;       
        private int[] _wayPoint;
        private WaveManager _waves;
        public WaveManager Waves
        {
            get { return _waves; }
            set { _waves = value; }
        }

        public Level(DxInitGraphics graphics,string mapFilePath, string mapImagePath, int cellWidth, int cellHeight)
        {
            _graphics = graphics;
            _cellWidth = cellWidth;
            _cellHeight = cellHeight;
            _texture = new DxSprite(mapImagePath, _graphics.GraphicsDevice, cellWidth, cellHeight);
          
           
           
            StreamReader sr = new StreamReader(mapFilePath);
            string data;
            string[] array;
            for (int line = 0; sr.EndOfStream == false; line++)
            {
                data = sr.ReadLine();
                if (data == "data=")
                {                    
                    for(int i = 0; i < 12; i++)
                    {
                        line++;
                        data = sr.ReadLine();
                        array = data.Split(',');
                        for(int j = 0; j < 16; j++)
                        {
                            if (line < 20)
                            {
                                _tileMap[i, j] = Convert.ToInt32(array[j]);
                            }
                            else
                            {
                                _colisonMap[i, j] = Convert.ToInt32(array[j]);
                            }
                        }
                    }
                }
                if (data == "waypoint")
                {
                    while (sr.EndOfStream == false)
                    {
                        line++;
                        data = sr.ReadLine();
                        array = data.Split(',');
                        _wayPoint = new int[array.Length];
                        for (int j = 0; j < array.Length; j++)
                        {
                            _wayPoint[j] = Convert.ToInt32(array[j]);
                        }
                    }
                }
            }
            sr.Close();
            _waves = new WaveManager(_graphics, "Sprite/robo.png", 15,_wayPoint);
            _waves.WayPoint = _wayPoint;

        }

        public void Update(double deltaTime, Player player)
        {
            _waves.Update(deltaTime, player);
        }
        public void Draw(Surface destSurface, Point position)
        {
            for (int i = 0; i < _tileMap.GetLength(0); i++)
            {
                for (int j = 0; j < _tileMap.GetLength(1); j++)
                {
                    if (_tileMap[i, j] != 0)
                    {
                        _texture.DrawFast(j * _cellWidth +3, i * _cellHeight +96, _tileMap[i, j], destSurface, DrawFastFlags.Wait);
                    }
                }
            }
            _waves.Draw(destSurface);           
        }
    }
}
